package cn.skyquant.quant4j.jforex.sdk.strategy;

import cn.skyquant.quant4j.jforex.sdk.enums.LabelType;
import cn.skyquant.quant4j.sdk.enums.TradeDirection;
import com.dukascopy.api.IOrder;
import com.dukascopy.api.Instrument;

import java.util.Objects;

public class OrderEx<T extends BaseOrderComment> {
    public String name;
    public LabelType labelType;//N:普通开仓,M:合并开仓
    public int configIndex = -1;//第几个配置
    public int seriesIndex = -1;//加到第几仓
    public Instrument instrument;//第几个货币
    public TradeDirection tradeDirection;
    public IOrder order;
    public T orderComment;
    public int r;//随机数，避免重复

    public int getSeriesIndex() {
        return seriesIndex;
    }

    public boolean valid(String name) {
        return name != null && this.name != null && name.equals(this.name)
                && labelType != null
                && configIndex != -1
                && seriesIndex != -1
                && instrument != null
                && tradeDirection != null
                && orderComment != null
                && orderComment.valid;
    }

    public boolean valid(String name, int configIndex) {
        return name != null && this.name != null && name.equals(this.name)
                && labelType != null
                && this.configIndex == configIndex
                && seriesIndex != -1
                && instrument != null
                && tradeDirection != null
                && orderComment != null
                && orderComment.valid;
    }

    public OrderEx(IOrder order, T t) {
        if (order != null && order.getLabel() != null) {
            this.order = order;
            String label = order.getLabel();
            String[] label_arr = label.split("_");
            if (label_arr != null
                    && label_arr.length >= 6
                    && label_arr[0] != null && label_arr[0].trim().length() > 0
                    && label_arr[1] != null && (label_arr[1].trim().equals(LabelType.N.name()) || label_arr[1].trim().equals(LabelType.M.name()))
                    && label_arr[2] != null && label_arr[2].trim().length() > 0
                    && label_arr[3] != null && label_arr[3].trim().length() > 0
                    && label_arr[4] != null && label_arr[4].trim().length() > 1
                    && label_arr[5] != null && (label_arr[5].trim().equals(TradeDirection.LONG.name()) || label_arr[5].trim().equals(TradeDirection.SHORT.name()))) {
                this.name = label_arr[0];//N:普通开仓,M:合并开仓
                try {
                    labelType = LabelType.valueOf(label_arr[1]);//N:普通开仓,M:合并开仓
                    configIndex = Integer.parseInt(label_arr[2]);//第几个配置
                    seriesIndex = Integer.parseInt(label_arr[3]);//加到第几仓
                    instrument = Instrument.valueOf(label_arr[4]);//货币
                    tradeDirection = TradeDirection.valueOf(label_arr[5]);//货币
                    t.parse(order.getComment());
                    this.orderComment = t;
                } catch (Exception e) {

                }
            }
            if (label_arr != null
                    && label_arr.length >= 7
                    && label_arr[6] != null && label_arr[6].trim().length() > 1){
                this.r = Integer.parseInt(label_arr[6]);
            }else {
                this.r = 0;
            }
        }
    }

    public OrderEx(String name, LabelType labelType, Integer configIndex, Integer seriesIndex, Instrument instrument, TradeDirection tradeDirection) {
        this.name = name;
        this.labelType = labelType;
        this.configIndex = configIndex;
        this.seriesIndex = seriesIndex;
        this.instrument = instrument;
        this.tradeDirection = tradeDirection;
        double r = Math.random();
        this.r = (int)(r*1000);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        OrderEx orderEx = (OrderEx) o;
        return configIndex == orderEx.configIndex &&
                seriesIndex == orderEx.seriesIndex &&
                Objects.equals(name, orderEx.name) &&
                labelType == orderEx.labelType &&
                Objects.equals(instrument, orderEx.instrument) &&
                tradeDirection == orderEx.tradeDirection;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, labelType, configIndex, seriesIndex, instrument, tradeDirection);
    }

    @Override
    public String toString() {
        return name + "_" + labelType + "_" + configIndex + "_" + seriesIndex + "_" + instrument.name() + "_" + tradeDirection.name()+"_"+r;
    }

    public Instrument getInstrument() {
        return instrument;
    }
}